<?php
namespace app\setting\controller;
use think\Request;
use think\Response;
use app\common\controller\AdminBase;

class ApiAdminUpload extends AdminBase
{
     public function defaultImgUpload(Request $request)
     {
        if (!Request::instance()->isPost()) {
            exit();
        }
            
        $file = $request->file('fileDataFileName');
        if (empty($file)) {
            return jsonError('图片不存在');
        }
        $tempFile = $file->getInfo();
        $ext = getImagetype($tempFile['tmp_name']);
        if ($ext !='png' && $ext!='jpeg' &&  $ext != 'gif' && $ext !='jpg') {
            return jsonError('图片类型错误');
        }
        $type = input('post.type');
        if (empty($type)) {
            $type = 'temp';
        }
        try {
            if ($ext == 'jpg') {
                $srcImage = imagecreatefromjpeg($tempFile['tmp_name']);
            }
            if ($ext == 'png') {
                $srcImage = imagecreatefrompng($tempFile['tmp_name']);
            }
            if ($ext == 'gif') {
                $srcImage = imagecreatefromgif($tempFile['tmp_name']);
            }
            $srcW = imagesx($srcImage);
            $srcH = imagesx($srcImage);
        } catch (\Exception $e) {
            return jsonError('图片上传失败');
        }
        $tempContent = file_get_contents($tempFile['tmp_name']);
        if (strpos($tempContent, '<?php') !== false) {
            return jsonError('图片上传失败');
        }
        if (strpos($tempContent, 'eval') !== false) {
            return jsonError('图片上传失败');
        }
        $name = 'uploads/' . $type;
        $aliyunossInfo = db('BaseAddons')->where('name','aliyunoss')->find();
        $qiniuossInfo = db('BaseAddons')->where('name','qiniuoss')->find();
        if ($aliyunossInfo || $qiniuossInfo) {
            if ($qiniuossInfo) {
                $title = uuid() . '.' . $ext;
                $res = model('addons\qiniuoss\model\QiniuossUploads')->defaultUpload($title,$tempContent,$name);
            }
            if ($aliyunossInfo) {
                $title = uuid() . '.' . $ext;
                $res = model('addons\aliyunoss\model\AliyunossUploads')->defaultUpload($title,$tempContent,$name);
            }
            if ($res['code'] == 1) {
                return jsonSuccess('ok',$res['data']);
            } else {
                return jsonError($res['msg']);
            }
        } else {
            if (SITE_HOST) {
                $public = 'public/';
            } else {
                $public = '';
            }
            if (!is_writable(PUBLIC_PATH . $this->settingInfo['uploadUrl'])) {
                return jsonError('/uploads 文件夹权限无法写入');
            }
            $uploadsPath  = PUBLIC_PATH . DS . $this->settingInfo['uploadUrl'] . DS . $type . DS . date('Y',time()) . DS . date('m',time()) . DS . date('d',time());
            $info = $file->rule('uniqid')->validate(['ext'=>'jpg,png,gif'])->move($uploadsPath);
            if ($info) {
                $domain = $this->settingInfo['runUrl'] ? config('domainStatic') : '';
                $data = $domain . '/' . $public . $this->settingInfo['uploadUrl'] .'/'.$type.'/' . date('Y',time()) . '/' . date('m',time()) . '/' . date('d',time()) . '/' . $info->getFilename();
                return jsonSuccess('',$data);
            } else {
                return jsonError($file->getError());
            }
        }
    }

}